﻿using System;
using System.Collections;
using System.Collections.Generic;
using FlowCanvas;
using FlowCanvas.Nodes;
using LogSystem;
using NodeCanvas.Framework;
using ParadoxNotion.Design;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI;

namespace Yoozoo.Gameplay.Liberty.StreetEvent
{
    public class ChangeNpcGroupStatus : CallableActionNode
    {
        [Serializable]
        public class NpcStatusSetting
        {
            public int streetNpcId;         // 街道npc的唯一Id
            public SideType sideType;
            public NpcStatus npcStatus;
            public bool autoRevenge;
            public bool resetToIdle;
        }
        
        public BBParameter<List<NpcStatusSetting>> settings;
        
        public override void Invoke()
        {
            AIEntity aiEntity = null;
            for (int i = 0; i < settings.value.Count; ++i)
            {
                NpcStatusSetting setting = settings.value[i];

                aiEntity = null;
                if (setting.streetNpcId != LibertyAIUtils.PLAYER_AI_ID)
                {
                    var m = StreetEventManager.GetInstance().GetMonster(setting.streetNpcId);
                    if (m != null)
                    {
                        aiEntity = LibertyAIUtils.AIManager.NpcBirthManager.GetNpc(m.uniqueId);
                    }
                }
                else
                {
                    aiEntity = LibertyAIUtils.PlayerController.playerEntity.AIEntity;
                }
                
                if (aiEntity != null)
                {
                    aiEntity.data.side = setting.sideType;
                    aiEntity.data.npcStatus = setting.npcStatus;

                    if (setting.resetToIdle)
                    {
                        aiEntity.viewer.animationAgent.PlayIdleAnimation();
                    }

                    if (setting.npcStatus == NpcStatus.Battle && setting.autoRevenge)
                    {
                        aiEntity.battleAgent.revengeTarget = LibertyAIUtils.GetPlayerEntity();
                        aiEntity.battleAgent.revengeColdDuration = -1;  // 永久仇恨
                    }
                }
                
            }
            
        }
    }
}